#ifndef _MENUS_C
#define _MENUS_C

#include "menus.h"


void draw_help_game_menu(int help){
    clear_bitmap(menu_buffer);
    textprintf_ex(menu_buffer,font,2,2,makecol(255,255,0),-1,"BASSERK - Version 1.2");
    if(help){
     textprintf_ex(menu_buffer,font,2,12,makecol(255,255,255),-1,"Arrow Keys move. LCTRL shoots");
     textprintf_ex(menu_buffer,font,2,22,makecol(255,255,255),-1,"Remake of Bezerk, with characters");
     textprintf_ex(menu_buffer,font,2,32,makecol(255,255,255),-1,"from Capcoms Megaman Series");
     textprintf_ex(menu_buffer,font,2,42,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,2,52,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,2,62,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,2,72,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,300,2,makecol(255,255,255),-1,"ESC - RETURN TO GAME/EXIT BACK TO MAIN");
     textprintf_ex(menu_buffer,font,300,12,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,300,22,makecol(255,255,255),-1," ");
     textprintf_ex(menu_buffer,font,300,32,makecol(255,255,255),-1,"******************************************");
     textprintf_ex(menu_buffer,font,300,42,makecol(255,255,255),-1,"* -                                    - *");
     textprintf_ex(menu_buffer,font,300,52,makecol(255,255,255),-1,"* -              NOTHING               - *");
     textprintf_ex(menu_buffer,font,300,62,makecol(255,255,255),-1,"* -                                    - *");
     textprintf_ex(menu_buffer,font,300,72,makecol(255,255,255),-1,"******************************************");
    }
    else{    
     textprintf_ex(menu_buffer,font,2,12,makecol(255,255,255),-1,"1/2 - -/+ ENEMY VELOCITY");
     textprintf_ex(menu_buffer,font,2,22,makecol(255,255,255),-1,"3/4 - -/+ BALL VELOCITY");
     textprintf_ex(menu_buffer,font,2,32,makecol(255,255,255),-1,"5/6 - -/+ ENEMY PROJECTILE VELOCITY");
     textprintf_ex(menu_buffer,font,2,42,makecol(255,255,255),-1,"D   - KILL/UN-KILL PLAYER");
     textprintf_ex(menu_buffer,font,2,52,makecol(255,255,255),-1,"E   - RE-INIT ENEMIES");
     textprintf_ex(menu_buffer,font,2,62,makecol(255,255,255),-1,"Q   - RE-INIT PLAYER");
     textprintf_ex(menu_buffer,font,2,72,makecol(255,255,255),-1,"Arrow Keys moves. LCTRL shoots");
     textprintf_ex(menu_buffer,font,300,2,makecol(255,255,255),-1,"ESC - RETURN TO TEST/EXIT BACK TO MAIN");
     textprintf_ex(menu_buffer,font,300,12,makecol(255,255,255),-1,"7/8 - -/+ BALL AMOUNT");
     textprintf_ex(menu_buffer,font,300,22,makecol(255,255,255),-1,"S - Show collision boxes");
     textprintf_ex(menu_buffer,font,300,32,makecol(255,255,255),-1,"******************************************");
     textprintf_ex(menu_buffer,font,300,42,makecol(255,255,255),-1,"* -                                    - *");
     textprintf_ex(menu_buffer,font,300,52,makecol(255,255,255),-1,"* -               NOTHING              - *");
     textprintf_ex(menu_buffer,font,300,62,makecol(255,255,255),-1,"* -                                    - *");
     textprintf_ex(menu_buffer,font,300,72,makecol(255,255,255),-1,"******************************************");
    }    
}

void draw_game_menu(void){
    int i,blink=PLAYER_PROFILE0,enem=ENEMY_PROFILE0,life_loc=6;
    if(local[0].hit||local[0].dead)blink=PLAYER_PROFILE3;
    else {
      if(local[0].wait>0&&local[0].wait<100)blink=PLAYER_PROFILE0;
      if(local[0].wait>100&&local[0].wait<105)blink=PLAYER_PROFILE0;
      if(local[0].wait>105&&local[0].wait<110)blink=PLAYER_PROFILE1;
      if(local[0].wait>110&&local[0].wait<115)blink=PLAYER_PROFILE2;
      if(local[0].wait>115&&local[0].wait<120)blink=PLAYER_PROFILE1;
      if(local[0].wait>120&&local[0].wait<125)blink=PLAYER_PROFILE0;
      if(local[0].wait>125&&local[0].wait<325)blink=PLAYER_PROFILE0;
      if(local[0].wait>325&&local[0].wait<330)blink=PLAYER_PROFILE0;
      if(local[0].wait>330&&local[0].wait<335)blink=PLAYER_PROFILE1;
      if(local[0].wait>335&&local[0].wait<340)blink=PLAYER_PROFILE2;
      if(local[0].wait>340&&local[0].wait<345)blink=PLAYER_PROFILE1;
      if(local[0].wait>345&&local[0].wait<350)blink=PLAYER_PROFILE0;
      if(local[0].wait>350&&local[0].wait<355)blink=PLAYER_PROFILE1;
      if(local[0].wait>355&&local[0].wait<360)blink=PLAYER_PROFILE2;
      if(local[0].wait>360&&local[0].wait<365)blink=PLAYER_PROFILE1;
      if(local[0].wait>365&&local[0].wait<370)blink=PLAYER_PROFILE0;
    }    
    if(enemy[show_enemy].hit){
        enemy[show_enemy].hit++;
        if(enemy[show_enemy].hit>0&&enemy[show_enemy].hit<30)enem=ENEMY_PROFILE1;
        else {
            enem=ENEMY_PROFILE0;
            enemy[show_enemy].hit=0;
        }
    }        
    if(enemy[show_enemy].dying)enem=ENEMY_PROFILE2;
    if(!enemy[show_enemy].alive)enem=ENEMY_PROFILE2;
    blit(status[GAME_MENU].dat,menu_buffer,0,0,0,0,EDIT_MENU_SIZE_W,EDIT_MENU_SIZE_H);
    blit(status[blink].dat,menu_buffer,0,0,6,6,48,48);
    if(local[0].lives>6){
        for(i=0;i<6;i++){
           blit(status[LIVES].dat,menu_buffer,0,0,life_loc,50,24,25);
          life_loc+=24;
        }
    }
    else {      
        for(i=0;i<local[0].lives;i++){
           blit(status[LIVES].dat,menu_buffer,0,0,life_loc,50,24,25);
           life_loc+=24;
        }
    }
    rect(menu_buffer,286,6,370,14,makecol(rand()%256,rand()%256,rand()%256)); 
    textprintf_centre_ex(menu_buffer,font,328,7,makecol(255,255,255),-1,"LEVEL %d",count_levels);     
    rect(menu_buffer,256,62,400,72,makecol(rand()%256,rand()%256,rand()%256));   
    textout_centre_ex(menu_buffer,font,"Press F1 For Help",328,64,makecol(255,255,255),-1);
    if(show_enemy_time){
        show_enemy_time++;
        blit(status[enem].dat,menu_buffer,0,0,586,6,48,48);
        if(show_enemy_time>=200)show_enemy_time=0;
    }    
    if(game_mode){
        blit(status[PLAYER_BAR].dat,menu_buffer,0,0,56,6,77,16);
        for(i=0;i<local[0].energy;i+=3){
            blit(status[BAR_FILL].dat,menu_buffer,0,0,74+i,6,3,16);
        }
        if(show_enemy_time){
            blit(status[ENEMY_BAR].dat,menu_buffer,0,0,509,6,77,16);
            for(i=0;i<enemy[show_enemy].energy;i+=3){
             blit(status[BAR_FILL].dat,menu_buffer,0,0,527+i,6,3,16);
            }
        }
        textout_centre_ex(menu_buffer,font,"SCORE",100,25,makecol(255,255,255),-1);
        textprintf_centre_ex(menu_buffer,font,100,35,makecol(255,255,255),-1,"%d",local[0].points);
        textprintf_centre_ex(menu_buffer,font,100,45,makecol(255,255,255),-1,"1UP @ %d",free_life);
        masked_blit(status[CHANGED].dat,menu_buffer,0,0,157,16,342,36);
    }        
    else {
        textout_centre_ex(menu_buffer,font,"SCORE",100,10,makecol(255,255,255),-1);
        textprintf_centre_ex(menu_buffer,font,100,20,makecol(255,255,255),-1,"%d",local[0].points);
        textprintf_centre_ex(menu_buffer,font,100,30,makecol(255,255,255),-1,"1UP @ %d",free_life);
        masked_blit(status[CLASSIC].dat,menu_buffer,0,0,220,16,342,36);
    }
    vline(menu_buffer,line1x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line2x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line3x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line4x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line5x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line6x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line7x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line8x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line9x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line10x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line11x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
    vline(menu_buffer,line12x1,liney1,liney2,makecol(rand()%256,rand()%256,rand()%256));
}

void add_points(void){
    if(point_adder>0){
     local[0].points++;
     point_adder--;
     play_sample(game_sound[POINT_TICK].dat,sound_vol,128,1000,0);
    }   
} 

void draw_pause(void){
    pause_wait--;
      if(pause_wait==0){  
         switch((rand()%3)+1){
            case 1:
              pause_speedx+=2;
              break;
            case 2:
              pause_speedx+=-2;
              break;
            case 3:
              pause_speedy+=2;
              break;
            case 4:
              pause_speedy+=-2;
              break;
         } 
         pause_wait=rand()%100;      
      }    
    pausex+=pause_speedx;
    pausey+=pause_speedy;
    if(pausex<=0){
        pausex=0;
        pause_speedx=2;
        switch((rand()%2)+1){
            case 1:
                pause_speedy=-2;
                break;
            case 2:
                pause_speedy=0;
                break;
            case 3:
                pause_speedy=2;
                break;
        }    
    }    
    if(pausex>=515){
        pausex=515;
        pause_speedx=-2;
        switch((rand()%2)+1){
            case 1:
                pause_speedy=-2;
                break;
            case 2:
                pause_speedy=0;
                break;
            case 3:
                pause_speedy=2;
                break;
        }   
    }    
    if(pausey<=0){
        pausey=0;
        pause_speedy=2;
        switch((rand()%2)+1){
            case 1:
                pause_speedx=-2;
                break;
            case 2:
                pause_speedx=0;
                break;
            case 3:
                pause_speedx=2;
                break;
        }   
    }    
    if(pausey>=340){
        pausey=340;
        pause_speedy=-2;
        switch((rand()%2)+1){
            case 1:
                pause_speedx=-2;
                break;
            case 2:
                pause_speedx=0;
                break;
            case 3:
                pause_speedx=2;
                break;
        }   
    }    
    masked_blit(status[PAUSE].dat,buffer,0,0, pausex, pausey,125,50);
}    


void screen_fade_in(BITMAP *bmp_orig, int speed)
{
    BITMAP *bmp_buff;

    if ((bmp_buff = create_bitmap(SCREEN_SIZE_W, SCREEN_SIZE_H)))
    {
        int a;
        if (speed <= 0) speed = 16;
        
        for (a = 0; a < 256; a+=speed)
        {
            clear(bmp_buff);
            set_trans_blender(0,0,0,a);
            draw_trans_sprite(bmp_buff, bmp_orig, 0, 0);
            vsync();
            blit(bmp_buff, screen, 0,0, 0,0, SCREEN_SIZE_W, SCREEN_SIZE_H); 
        }
        destroy_bitmap(bmp_buff);
    }
    acquire_screen();
    blit(bmp_orig, screen, 0,0, 0,0, SCREEN_SIZE_W, SCREEN_SIZE_H);
    release_screen();
}

void screen_fade_out(int speed)
{
    BITMAP *bmp_orig, *bmp_buff;

    if ((bmp_orig = create_bitmap(SCREEN_SIZE_W, SCREEN_SIZE_H)))
    {
        if ((bmp_buff = create_bitmap(SCREEN_SIZE_W, SCREEN_SIZE_H)))
        {
            int a;
            blit(screen, bmp_orig, 0,0, 0,0, SCREEN_SIZE_W, SCREEN_SIZE_H);
            if (speed <= 0) speed = 16;
        
            for (a = 255-speed; a > 0; a-=speed)
            {
                clear(bmp_buff);
                set_trans_blender(0,0,0,a);
                draw_trans_sprite(bmp_buff, bmp_orig, 0, 0);
                vsync();
                blit(bmp_buff, screen, 0,0, 0,0, SCREEN_SIZE_W, SCREEN_SIZE_H);
            }
            destroy_bitmap(bmp_buff);
        }
        destroy_bitmap(bmp_orig);
    }

    rectfill(screen, 0,0, SCREEN_SIZE_W,SCREEN_SIZE_H, makecol(0,0,0));
}

void draw_game_over(BITMAP *gameover_bmp){
    int done=0,i,scale_x=198,scale_y=41,scaleme=3,end_gameover=500;
    float gameover_x=221,gameover_y=-50;
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    BITMAP *back_buff;
    back_buff=create_bitmap(640,400);
    for(i=0;i<14;i++){
          stop_sample(game_sound[i].dat);
    }
    play_sample(game_sound[GAME_OVER_MUSIC].dat,music_vol,128,1000,1);
    process=1;
    while(!done){
        while(game_timer){
           if(key[KEY_ESC]||key[KEY_ENTER]){
             keyhold[KEY_ESC]=1;
             keyhold[KEY_ENTER]=1;
             done=1;
           }    
           if(scaleme==3&&gameover_y<180)gameover_y+=4;
           if(scaleme==3&&gameover_y>=180)gameover_y=180;
           if(scaleme==3&&gameover_y==180)scaleme=1;
           if(scaleme==1&&scale_x>=398&&scale_y>=341)scaleme=2;
           if(scaleme==1){
             scale_x+=4;scale_y+=4;
             gameover_x-=2;gameover_y-=2;
           }
           if(scaleme==2&&scale_x<=198&&scale_y<=41)scaleme=1;
           if(scaleme==2) {
             scale_x-=4;scale_y-=4;
             gameover_x+=2;gameover_y+=2;
           }
           end_gameover--;
           game_timer--;       
         }  
         if(end_gameover<=0)done=1;   
         blit(gameover_bmp,back_buff,0,0,0,0,640,400);
         stretch_sprite(back_buff,status[GAME_OVER].dat, gameover_x, gameover_y,scale_x,scale_y);
         draw_buffer(back_buff,0,0,0,0,EDIT_MAP_SIZE_W,EDIT_MAP_SIZE_H);
         //sleep(1);
    }    
    process=0;
    check_highscore(back_buff);
    screen_fade_out(10);
    destroy_bitmap(back_buff);
}  

void save_highscore(void){
    PACKFILE *pfile;
    if((pfile= pack_fopen("data/basshigh.dat","wp"))){  
       pack_fwrite(score,sizeof(score),pfile);
       pack_fwrite(type,sizeof(type),pfile);
       pack_fwrite(name,sizeof(name),pfile); 
       pack_fwrite(level,sizeof(level),pfile);
       pack_fwrite(difficulty,sizeof(difficulty),pfile);
       pack_fclose(pfile);
    }       
}    

void load_highscore(void){
    PACKFILE *pfile;
    if((pfile= pack_fopen("data/basshigh.dat","rp"))){
        pack_fread(score,sizeof(score),pfile);
        pack_fread(type,sizeof(type),pfile);
        pack_fread(name,sizeof(name),pfile);  
        pack_fread(level,sizeof(level),pfile);
        pack_fread(difficulty,sizeof(difficulty),pfile);
        pack_fclose(pfile);
    }
    else {
        strcpy(name[5],"CAPCOM");
        score[5]=20000;
        type[5]=1;
        level[5]=50;
        strcpy(difficulty[5],"Very Hard");
        strcpy(name[4],"WILY");
        score[4]=15000;
        type[4]=0;
        level[4]=50;
        strcpy(difficulty[4],"Hard");
        strcpy(name[3],"LIGHT");
        score[3]=10000;
        type[3]=1;
        level[3]=40;
        strcpy(difficulty[3],"Hard");
        strcpy(name[2],"PROTO");
        score[2]=8000;
        type[2]=0;
        level[2]=30;
        strcpy(difficulty[2],"Normal");
        strcpy(name[1],"RUSH");
        score[1]=5000;
        type[1]=1;
        level[1]=20;
        strcpy(difficulty[1],"Easy");
    }        
}

void check_highscore(BITMAP *back){
   int i,j=0,ch,done=0,type_stop=0;
   char temp_name[5]={0,0,0,0,0},
        char_buf[2]={0,0};
   for(i=5;i>0;i--){
        if(local[0].points>=score[i])break;
   }
   if(i>0){
        BITMAP *buff;
        buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
        clear_keybuf();
        while(!done){
            blit(back,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
            textprintf_centre_ex(buff,game_fonts[1].dat,320,250,makecol(rand()%255,rand()%255,rand()%255),-1,"I N P U T   N A M E");
            textprintf_centre_ex(buff,game_fonts[1].dat,320,300,makecol(255,255,255),-1,"%s",(char *)temp_name);
            draw_buffer(buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
            ch = readkey();          
            if((ch&0xff)>='A'&&(ch&0xff)<='Z'){
                 sprintf(char_buf,"%c",(ch&0xff));
                 strcat(temp_name,char_buf);
                 type_stop++;
            }
            else if((ch&0xff)>='a'&&(ch&0xff)<='z'){
                   sprintf(char_buf,"%c",toupper((ch&0xff)));
                   strcat(temp_name,char_buf);
                   type_stop++;
            }        
            else if((ch&0xff)==8){
                temp_name[strlen(temp_name)-1]=0;
                type_stop--;
            } 
            else if((ch&0xff)==13){
                keyhold[KEY_ENTER]=1;
                done=1;
            }   
            if(type_stop>8){
                type_stop=8;
                temp_name[strlen(temp_name)-1]=0;
            }    
            if(type_stop<0)type_stop=0;
        } 
        for(j=0;j<i;j++){
            strcpy(name[j],name[j+1]);
            score[j]=score[j+1];
            type[j]=type[j+1]; 
            strcpy(difficulty[j],difficulty[j+1]);
            level[j]=level[j+1];
        }
        strcpy(name[i],temp_name);
        score[i]=local[0].points;
        type[i]=game_mode;  
        level[i]=count_levels;
        switch(game_difficulty){
            case 0:
                strcpy(difficulty[i],"Easy");
                break;
            case 1:
                strcpy(difficulty[i],"Normal");
                break;
            case 2:
                strcpy(difficulty[i],"Hard");
                break;
            case 3:
                strcpy(difficulty[i],"Very Hard");
                break;
        }    
        high_score_show=score[i];  
        save_highscore();      
        destroy_bitmap(buff);   
   }            
}       

int draw_highscore(void){
    int wait=0,done=0,end_high=1000, main_return=0,high_x1=0,high_x2=640,
        main_x=320,main_y=400,color_count=0,
        first_x=10,first_y=480,blue1=255,grn1=255,
        second_x=10,second_y=540,blue2=0,grn2=255,
        third_x=10,third_y=600,blue3=0,grn3=255,
        fourth_x=10,fourth_y=660,blue4=0,grn4=255,
        fifth_x=10,fifth_y=720,blue5=0,grn5=255;
    BITMAP *buff,*sec_buff;
    DATAFILE *highscore,*status;
    buff= create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    sec_buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    highscore= load_datafile("data/title.dat");
    status=load_datafile("data/status.dat");
    process=1;
    while(!done){
        while(game_timer){
            if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
                keyhold[KEY_ESC]=1;
                done=1;
                main_return=1;
            }
            else {
                if(!key[KEY_ESC]){
                    keyhold[KEY_ESC]=0;
                }
            }            
            high_x1+=2;
            high_x2-=2;
            if(high_x1>=640)high_x1=0;
            if(high_x2<=0)high_x2=640;
            if(main_y>20){
                main_y-=2;first_y-=2;second_y-=2;third_y-=2;fourth_y-=2;fifth_y-=2;
            }
            if(main_y<20)main_y=20;
            if(color_count>0&&color_count<20){
                 blue1=255;blue2=0;blue3=0;blue4=0;blue5=0;
            }
            if(color_count>20&&color_count<40){
                 blue1=0;blue2=255;blue3=0;blue4=0;blue5=0;
            }    
            if(color_count>40&&color_count<60){
                 blue1=0;blue2=0;blue3=255;blue4=0;blue5=0;
            }
            if(color_count>60&&color_count<80){
                 blue1=0;blue2=0;blue3=0;blue4=255;blue5=0;
            }    
            if(color_count>80&&color_count<100){
                 blue1=0;blue2=0;blue3=0;blue4=0;blue5=255;
            }  
            if(color_count>100)color_count=0;
            if(score[5]==high_score_show){
                    if(color_count>0&&color_count<25){
                     grn1=0;
                    }
                    if(color_count>25&&color_count<50){
                     grn1=255;
                    }
                    if(color_count>50&&color_count<75){
                     grn1=0;
                    }
                    if(color_count>75&&color_count<100){
                     grn1=255;
                    }    
            }    
            if(score[4]==high_score_show){
                    if(color_count>0&&color_count<25){
                     grn2=0;
                    }
                    if(color_count>25&&color_count<50){
                     grn2=255;
                    }
                    if(color_count>50&&color_count<75){
                     grn2=0;
                    }
                    if(color_count>75&&color_count<100){
                     grn2=255;
                    }        
            }         
            if(score[3]==high_score_show){
                    if(color_count>0&&color_count<25){
                     grn3=0;
                    }
                    if(color_count>25&&color_count<50){
                     grn3=255;
                    }
                    if(color_count>50&&color_count<75){
                     grn3=0;
                    }
                    if(color_count>75&&color_count<100){
                     grn3=255;
                    }        
            } 
            if(score[2]==high_score_show){
                    if(color_count>0&&color_count<25){
                     grn4=0;
                    }
                    if(color_count>25&&color_count<50){
                     grn4=255;
                    }
                    if(color_count>50&&color_count<75){
                     grn4=0;
                    }
                    if(color_count>75&&color_count<100){
                     grn4=255;
                    }    
            } 
            if(score[1]==high_score_show){
                    if(color_count>0&&color_count<25){
                     grn5=0;
                    }
                    if(color_count>25&&color_count<50){
                     grn5=255;
                    }
                    if(color_count>50&&color_count<75){
                     grn5=0;
                    }
                    if(color_count>75&&color_count<100){
                     grn5=255;
                    }      
            } 
            color_count++;  
            end_high--;
            game_timer--;
        }
        if(wait<256)wait+=10;
        if(end_high<0)done=1;  
        blit(highscore[HIGHSCORE].dat,buff,high_x1,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        blit(highscore[HIGHSCORE].dat,buff,0,0,high_x2,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        textprintf_centre_ex(buff,game_fonts[1].dat,main_x,main_y,makecol(0,0,255),-1,"T O P  F I V E");
        textprintf_ex(buff,game_fonts[1].dat,first_x,first_y,makecol(255,grn1,blue1),-1,"%s",(char *)name[5]);
        textprintf_centre_ex(buff,game_fonts[1].dat,320,first_y,makecol(255,grn1,blue1),-1,"%d",score[5]);
        textprintf_centre_ex(buff,font,475,first_y,makecol(255,grn1,blue1),-1,"Level %d",level[5]);
        textprintf_centre_ex(buff,font,475,first_y+15,makecol(255,grn1,blue1),-1,"%s",(char *)difficulty[5]);
        rect(buff,527,first_y-3,633,first_y+25,makecol(255,grn1,blue1));
        rectfill(buff,528,first_y-2,632,first_y+24,makecol(0,0,0));
        if(type[5])stretch_sprite(buff,status[CHANGED].dat,530,first_y,100,20);
        else stretch_sprite(buff,status[CLASSIC].dat,530,first_y,100,20);
        textprintf_ex(buff,game_fonts[1].dat,second_x,second_y,makecol(255,grn2,blue2),-1,"%s",(char *)name[4]);
        textprintf_centre_ex(buff,game_fonts[1].dat,320,second_y,makecol(255,grn2,blue2),-1,"%d",score[4]);
        textprintf_centre_ex(buff,font,475,second_y,makecol(255,grn2,blue2),-1,"Level %d",level[4]);
        textprintf_centre_ex(buff,font,475,second_y+15,makecol(255,grn2,blue2),-1,"%s",(char *)difficulty[4]);
        rect(buff,527,second_y-3,633,second_y+25,makecol(255,grn2,blue2));
        rectfill(buff,528,second_y-2,632,second_y+24,makecol(0,0,0));
        if(type[4])stretch_sprite(buff,status[CHANGED].dat,530,second_y,100,20);
        else stretch_sprite(buff,status[CLASSIC].dat,530,second_y,100,20);
        textprintf_ex(buff,game_fonts[1].dat,third_x,third_y,makecol(255,grn3,blue3),-1,"%s",(char *)name[3]);
        textprintf_centre_ex(buff,game_fonts[1].dat,320,third_y,makecol(255,grn3,blue3),-1,"%d",score[3]);
        textprintf_centre_ex(buff,font,475,third_y,makecol(255,grn3,blue3),-1,"Level %d",level[3]);
        textprintf_centre_ex(buff,font,475,third_y+15,makecol(255,grn3,blue3),-1,"%s",(char *)difficulty[3]);
        rect(buff,527,third_y-3,633,third_y+25,makecol(255,grn3,blue3));
        rectfill(buff,528,third_y-2,632,third_y+24,makecol(0,0,0));
        if(type[3])stretch_sprite(buff,status[CHANGED].dat,530,third_y,100,20);
        else stretch_sprite(buff,status[CLASSIC].dat,530,third_y,100,20);
        textprintf_ex(buff,game_fonts[1].dat,fourth_x,fourth_y,makecol(255,grn4,blue4),-1,"%s",(char *)name[2]);
        textprintf_centre_ex(buff,game_fonts[1].dat,320,fourth_y,makecol(255,grn4,blue4),-1,"%d",score[2]);
        textprintf_centre_ex(buff,font,475,fourth_y,makecol(255,grn4,blue4),-1,"Level %d",level[2]);
        textprintf_centre_ex(buff,font,475,fourth_y+15,makecol(255,grn4,blue4),-1,"%s",(char *)difficulty[2]);
        rect(buff,527,fourth_y-3,633,fourth_y+25,makecol(255,grn4,blue4));
        rectfill(buff,528,fourth_y-2,632,fourth_y+24,makecol(0,0,0));
        if(type[2])stretch_sprite(buff,status[CHANGED].dat,530,fourth_y,100,20);
        else stretch_sprite(buff,status[CLASSIC].dat,530,fourth_y,100,20);
        textprintf_ex(buff,game_fonts[1].dat,fifth_x,fifth_y,makecol(255,grn5,blue5),-1,"%s",(char *)name[1]);
        textprintf_centre_ex(buff,game_fonts[1].dat,320,fifth_y,makecol(255,grn5,blue5),-1,"%d",score[1]);
        textprintf_centre_ex(buff,font,475,fifth_y,makecol(255,grn5,blue5),-1,"Level %d",level[1]);
        textprintf_centre_ex(buff,font,475,fifth_y+15,makecol(255,grn5,blue5),-1,"%s",(char *)difficulty[1]);
        rect(buff,527,fifth_y-3,633,fifth_y+25,makecol(255,grn5,blue5));
        rectfill(buff,528,fifth_y-2,632,fifth_y+24,makecol(0,0,0));
        if(type[1])stretch_sprite(buff,status[CHANGED].dat,530,fifth_y,100,20);
        else stretch_sprite(buff,status[CLASSIC].dat,530,fifth_y,100,20);
         
        if(wait<=256){
           clear(sec_buff);
           set_trans_blender(0,0,0,wait);
           draw_trans_sprite(sec_buff, buff, 0, 0);
           blit(sec_buff,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        }
        draw_buffer(buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        //sleep(1);
    }    
    process=0;
    screen_fade_out(10);
    if(high_score_show)high_score_show=0;
    stop_sample(game_sound[GAME_OVER_MUSIC].dat);
    destroy_bitmap(buff);
    destroy_bitmap(sec_buff);    
    unload_datafile(highscore);
    unload_datafile(status);
    return main_return=0;
}    

void screen_close(BITMAP *bmp){
    int updown,ended=0,
        rect1x1=0,rect1x2=0,rect1y1=0,rect1y2=0,
        rect2x1=0,rect2x2=0,rect2y1=0,rect2y2=0;
    updown=(rand()%5)+1;
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    switch(updown){
        case 1:
            rect1x1=0;rect1x2=640;
            rect1y1=0;rect1y2=0;
            break;
        case 2:
            rect2x1=0;rect2x2=640;
            rect2y1=400;rect2y2=400;
            break;
        case 3:
            rect1x1=0;rect1x2=640;
            rect1y1=0;rect1y2=0;
            rect2x1=0;rect2x2=640;
            rect2y1=400;rect2y2=400;
            break;
        case 4:
            rect1x1=0;rect1x2=0;
            rect1y1=0;rect1y2=400;
            break;
        case 5:
            rect2x1=640;rect2x2=640;
            rect2y1=0;rect2y2=400;
            break;
        case 6:
            rect1x1=0;rect1x2=0;
            rect1y1=0;rect1y2=400;
            rect2x1=640;rect2x2=640;
            rect2y1=0;rect2y2=400;
            break;
    }
    process=1;
    while(!ended){
      while(game_timer){
        if(updown==1){
            rect1y2+=4;
            if(rect1y2>=400)ended=1;
        }
        if(updown==2){
            rect2y2-=4;
            if(rect2y2<=0)ended=1;
        }        
        if(updown==3){
            rect1y2+=2;
            rect2y2-=2;
            if(rect1y2>=200&&rect2y2<=200)ended=1;
        }
        if(updown==4){
            rect1x2+=4;
            if(rect1x2>=640)ended=1;
        }
        if(updown==5){
            rect2x2-=4;
            if(rect2x2<=0)ended=1;
        }
        if(updown==6){
            rect1x2+=2;
            rect2x2-=2;
            if(rect1x2>=320&&rect2x2<=320)ended=1;
        } 
        add_points();
        if(local[0].points>=free_life){
          local[0].lives++;
          play_sample(game_sound[FREE_MAN].dat,sound_vol,128,1000,0);
          free_life+=free_life+1000;
        }   
        game_timer--;
      }    
      blit(bmp,buffer,0,0,0,0,640,400);
      rectfill(buffer,rect1x1,rect1y1,rect1x2,rect1y2,makecol(0,0,0));
      rectfill(buffer,rect2x1,rect2y1,rect2x2,rect2y2,makecol(0,0,0));
      draw_buffer(buffer,0,0,0,0,EDIT_MAP_SIZE_W,EDIT_MAP_SIZE_H);  
      draw_game_menu();
      draw_buffer(menu_buffer,0,0,0,EDIT_MAP_SIZE_H,EDIT_MENU_SIZE_W,EDIT_MENU_SIZE_H);
      //sleep(1);
    }    
    process=0;
}

int yesno_win(BITMAP *screen_capture,char *txt,int x,int y){
   int fin=0,yesno=0,selectme=0,
       sel_x1=x+70,sel_x2=x+105,sel_y1=y+45,sel_y2=y+60;
   BITMAP *buff;
   buff= create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
   while(!fin){
    if(key[KEY_LEFT]&&!keyhold[KEY_LEFT]){
        keyhold[KEY_LEFT]=1;
        if(selectme){
            selectme=0;
            sel_x1=x+70;sel_x2=x+105;
            sel_y1=y+45;sel_y2=y+60;
        }    
        else {
            selectme=1;
            sel_x1=x+20;sel_x2=x+55;
            sel_y1=y+45;sel_y2=y+60;
        }    
    }
    else {
        if(!key[KEY_LEFT]){
            keyhold[KEY_LEFT]=0;
        }
    }        
    if(key[KEY_RIGHT]&&!keyhold[KEY_RIGHT]){
        keyhold[KEY_RIGHT]=1;
        if(selectme){
            selectme=0;
            sel_x1=x+70;sel_x2=x+105;
            sel_y1=y+45;sel_y2=y+60;
        }    
        else {
            selectme=1;
            sel_x1=x+20;sel_x2=x+55;
            sel_y1=y+45;sel_y2=y+60;
        }   
    }
    else {
        if(!key[KEY_RIGHT]){
            keyhold[KEY_RIGHT]=0;
        }
    }        
    if(key[KEY_ENTER]&&!keyhold[KEY_ENTER]){
        keyhold[KEY_ENTER]=1;
        if(selectme)yesno=1;
        else yesno=0;
        fin=1;
    }        
    else {
        if(!key[KEY_ENTER]){
            keyhold[KEY_ENTER]=0;
        }
    }          
    if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
        keyhold[KEY_ESC]=1;
        yesno=0;
        fin=1;
    }        
    else {
        if(!key[KEY_ESC]){
            keyhold[KEY_ESC]=0;
        }
    }        
    blit(screen_capture,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
    rect(buff,x-1,y-1,x+121,y+66,makecol(255,255,255));
    rectfill(buff,x,y,x+120,y+65,makecol(0,0,0));
    textprintf_centre_ex(buff,font,x+60,y+10,makecol(255,255,255),-1,"%s",txt);
    textprintf_centre_ex(buff,font,x+60,y+30,makecol(255,255,255),-1,"Are you sure?");
    textprintf_ex(buff,font,x+10,y+50,makecol(255,255,255),-1,"  YES    NO  ");
    rect(buff,sel_x1,sel_y1,sel_x2,sel_y2,makecol(0,255,0));
    blit(buff,screen,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
    //sleep(1);
   }    
   destroy_bitmap(buff);
   return yesno;
}     

void test_map(void){
    int i,j,ok=0,count=0,end_test=0,win_test=0;
    char *back="Back to Menu?";
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    save_temp_data();
    //(get_map_number(num));
    if(!load_test_map(buffer))return;
    init_player(1);
    init_enemies(enemy_count);
    init_ball(ball_count,ball_time);  
    play_sample(game_sound[GAME_MUSIC].dat,music_vol,128,1000,1);
    process=1;
     while(!end_test){ 
      while(game_timer){ 
       if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
           keyhold[KEY_ESC]=1;
           process=0;
           if(!help&&yesno_win(screen,back,259,174)){
               end_test=1;
           }
           if(help)help=0;
           process=1;
       }        
       else {
           if(!key[KEY_ESC]){
               keyhold[KEY_ESC]=0;
           }
       }        
       if(key[KEY_1])enemy_velocity-=.1;
       if(key[KEY_2])enemy_velocity+=.1;
       if(key[KEY_3])ball_velocity-=.1;
       if(key[KEY_4])ball_velocity+=.1;
       if(key[KEY_5])enemy_shot_vel-=.1;
       if(key[KEY_6])enemy_shot_vel+=.1;
       if(key[KEY_7]&&!keyhold[KEY_7]){
           keyhold[KEY_7]=1;
           ball_count--;
           if(ball_count<=0)ball_count=0;
           init_ball(ball_count,ball_time);
       }
       else {
           if(!key[KEY_7]){
               keyhold[KEY_7]=0;
           }
       }    
       if(key[KEY_8]&&!keyhold[KEY_8]){
           keyhold[KEY_8]=1;
           ball_count++;
           if(ball_count>=4)ball_count=4;
           init_ball(ball_count,ball_time);
       }    
       else {
           if(!key[KEY_8]){
               keyhold[KEY_8]=0;
           }
       }        
       if(key[KEY_D]){
           if(!local[0].dead){
               local[0].dead=1;play_sample(game_sound[P_ELEC].dat,sound_vol,128,1000,1);
           }    
           else {
               local[0].dead=0;stop_sample(game_sound[P_ELEC].dat);
           }    
       }    
       if(key[KEY_E]){
           stop_sample(game_sound[GAME_MUSIC].dat);
           enemy_count=(rand()%25)+10;
           init_enemies(enemy_count);
           init_ball(ball_count,ball_time);
           play_sample(game_sound[GAME_MUSIC].dat,music_vol,128,1000,1);
       }    
       if(key[KEY_S]&&!keyhold[KEY_S]){
           keyhold[KEY_S]=1;
           if(show_rects)show_rects=0;
           else show_rects=1;
       }
       else {
           if(!key[KEY_S]){
               keyhold[KEY_S]=0;
           }
       }               
       if(key[KEY_Q]){
           ok=0;init_player(1);
           while(!ok){
            for(j=0;j<MAX_ENEMY;j++){
                  if(enemy[j].alive){
                      if(enemy_col_player(enemy[j],local[0])){
                          init_player(1);break;
                      }
                  }
                  else ok=1;
            }
           }     
       } 
       if(key[KEY_F1]&&!keyhold[KEY_F1]){
           keyhold[KEY_F1]=1;
           if(help)help=0;
           else help=1;
       }
       else {
           if(!key[KEY_F1]){
               keyhold[KEY_F1]=0;
           }
       }            
       if(!local[0].dead&&!help){
           player_control(1);
           update_player_shot(1,enemy_count);
           move_enemies(enemy_count,enemy_velocity);
           enemy_shoot(enemy_count,local[0].x1,local[0].y1);
           update_enemy_shot(enemy_count,enemy_shot_vel);
           move_ball(ball_count,ball_velocity,local[0].cur_x,local[0].cur_y);
           update_enemy_explosion(enemy_count);
           update_ball_explosion(ball_count);
           add_points();
           for(i=0;i<MAX_ENEMY;i++){
               if(!enemy[i].alive)count++;
               else count=0;
           }  
           if(count>=MAX_ENEMY){
              if(open_doors()>0)play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0); 
              win_test++; 
           }    
           else {
               if(close_doors()>0)play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0);
               win_test=0;
           }    
           if(game_mode&&win_test==1){
               stop_sample(game_sound[GAME_MUSIC].dat);
               play_sample(game_sound[WIN_MUSIC].dat,music_vol,128,1000,0);
           }    
       }    
       if(local[0].dead)player_death(local[0].id);
       game_timer--;
      }
      draw_map();
      if(show_rects){
        for(i=0;i<25;i++){
              for(j=0;j<40;j++){
                   if(tiles[i][j].att)rect(buffer,tiles[i][j].x1,tiles[i][j].y1,tiles[i][j].x2,tiles[i][j].y2,makecol(255,255,255));
              }
       }
      }  
      draw_player(1);
      draw_enemies(enemy_count);
      draw_enemy_shot(enemy_count);
      draw_player_shot(1);
      draw_ball(ball_count);
      draw_enemy_explosion(enemy_count);
      draw_ball_explosion(ball_count);
      if(help)draw_pause();  
      draw_buffer(buffer,0,0,0,0,EDIT_MAP_SIZE_W,EDIT_MAP_SIZE_H);
      if(help)draw_help_game_menu(HELP_MAP_TEST);
      else draw_game_menu();
      draw_buffer(menu_buffer,0,0,0,EDIT_MAP_SIZE_H,EDIT_MENU_SIZE_W,EDIT_MENU_SIZE_H); 
      //sleep(1);
    }
    process=0;
    screen_close(buffer);
    for(i=0;i<11;i++){
     stop_sample(game_sound[i].dat);
    }    
    load_temp_data();
    local[0].points=0;
}   

int main_option_win(void){
    int count1=0,count2=0,spr=OPTION0,end_option=0,
        bar1_x1=295,bar1_x2=295+music_vol/3,bar1_y1=110,bar1_y2=119,
        bar2_x1=295,bar2_x2=295+sound_vol/3,bar2_y1=130,bar2_y2=139,
        selector=105,select_x=180,select_y=285,bass_x=5,bass_y=120,
        menu_x=220,menu_y=140,tester=0;
    char *def="Default Config?";
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    BITMAP *buff;
    DATAFILE *title_images;
    DATAFILE *option_menu;
    buff= create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    title_images=load_datafile("data/title.dat");
    option_menu = load_datafile("data/options.dat");
    process=1;
    while(end_option!=3){
      while(game_timer){
        if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
            keyhold[KEY_ESC]=1;
            end_option=2;
        }    
        else {
            if(!key[KEY_ESC]){
                keyhold[KEY_ESC]=0;
            }
        }        
        if(key[KEY_ENTER]&&!keyhold[KEY_ENTER]){
            keyhold[KEY_ENTER]=1;
            if(selector==185){
               tester=1;end_option=2;
            }    
            if(selector==205){
                process=0;
                if(yesno_win(screen,def,259,174)){
                   reset_default();
                   adjust_sample(game_sound[TITLE_MUSIC].dat,music_vol,128,1000,1);
                   bar1_x2=295+music_vol/3;
                   bar2_x2=295+sound_vol/3;
                }    
                process=1;
            }    
        }    
        else {
            if(!key[KEY_ENTER]){
                keyhold[KEY_ENTER]=0;
            }
        }        
        if(key[KEY_UP]&&!keyhold[KEY_UP]){
            keyhold[KEY_UP]=1;
            selector-=20;
            if(selector<105)selector=205;
            play_sample(game_sound[SELECT_SOUND].dat,sound_vol,128,1000,0);
        }
        else {
            if(!key[KEY_UP]){
                keyhold[KEY_UP]=0;
            }
        }
        if(key[KEY_DOWN]&&!keyhold[KEY_DOWN]){
            keyhold[KEY_DOWN]=1;
            selector+=20;
            if(selector>205)selector=105;
            play_sample(game_sound[SELECT_SOUND].dat,sound_vol,128,1000,0);
        }
        else {
            if(!key[KEY_DOWN]){
                keyhold[KEY_DOWN]=0;
            }
        }            
        if(key[KEY_LEFT]&&!keyhold[KEY_LEFT]){
            keyhold[KEY_LEFT]=1;
            switch(selector){
                case 105:
                    keyhold[KEY_LEFT]=0;
                    music_vol--;
                    if(music_vol<0)music_vol=0;
                    bar1_x2=295+music_vol/3;
                    adjust_sample(game_sound[TITLE_MUSIC].dat,music_vol,128,1000,1);
                    break;
                case 125:
                    keyhold[KEY_LEFT]=0;
                    sound_vol--;
                    if(sound_vol<0)sound_vol=0;
                    bar2_x2=295+sound_vol/3;
                    play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0);
                    break;
                case 145:
                    game_difficulty--;
                    if(game_difficulty<0)game_difficulty=3;
                    switch(game_difficulty){
                        case 0:
                            player_lives=4;
                            player_attack=10;
                            enemy_count=3;
                            enemy_velocity=.4;
                            enemy_shot_vel=.4;
                            enemy_attack=.5;
                            ball_count=0;
                            ball_velocity=0;
                            ball_time=0;
                            break;
                        case 1:
                            player_lives=3;
                            player_attack=6;
                            enemy_count=5;
                            enemy_velocity=.5;
                            enemy_shot_vel=.5;
                            enemy_attack=1;
                            ball_count=1;
                            ball_velocity=.5;
                            ball_time=1000;
                            break;
                        case 2:
                            player_lives=3;
                            player_attack=3;
                            enemy_count=7;
                            enemy_velocity=.6;
                            enemy_shot_vel=.6;
                            enemy_attack=2;
                            ball_count=2;
                            ball_velocity=.6;
                            ball_time=800;
                            break;
                        case 3:
                            player_lives=2;
                            player_attack=1;
                            enemy_count=10;
                            enemy_velocity=.7;
                            enemy_shot_vel=.7;
                            enemy_attack=3;
                            ball_count=3;
                            ball_velocity=.8;
                            ball_time=500;
                            break;
                    }    
                    break;
                case 165:
                    game_mode--;
                    if(game_mode<0)game_mode=1;
                    if(game_mode)map_number=1;
                    else map_number=21;
                    break;
           }
        }
        else {
            if(!key[KEY_LEFT]){
                keyhold[KEY_LEFT]=0;
            }
        }        
        if(key[KEY_RIGHT]&&!keyhold[KEY_RIGHT]){
            keyhold[KEY_RIGHT]=1;
            switch(selector){
                case 105:
                    keyhold[KEY_RIGHT]=0;
                    music_vol++;
                    if(music_vol>255)music_vol=255;
                    bar1_x2=295+music_vol/3;
                    adjust_sample(game_sound[TITLE_MUSIC].dat,music_vol,128,1000,1);
                    break;
                case 125:
                    keyhold[KEY_RIGHT]=0;
                    sound_vol++;
                    if(sound_vol>255)sound_vol=255;
                    bar2_x2=295+sound_vol/3;
                    play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0);
                    break;
                case 145:
                    game_difficulty++;
                    if(game_difficulty>3)game_difficulty=0;
                    switch(game_difficulty){
                        case 0:
                            player_lives=4;
                            player_attack=10;
                            enemy_count=3;
                            enemy_velocity=.4;
                            enemy_shot_vel=.4;
                            enemy_attack=.5;
                            ball_count=0;
                            ball_velocity=0;
                            ball_time=0;
                            break;
                        case 1:
                            player_lives=3;
                            player_attack=6;
                            enemy_count=5;
                            enemy_velocity=.5;
                            enemy_shot_vel=.5;
                            enemy_attack=1;
                            ball_count=1;
                            ball_velocity=.5;
                            ball_time=1000;
                            break;
                        case 2:
                            player_lives=3;
                            player_attack=3;
                            enemy_count=7;
                            enemy_velocity=.6;
                            enemy_shot_vel=.6;
                            enemy_attack=2;
                            ball_count=2;
                            ball_velocity=.6;
                            ball_time=800;
                            break;
                        case 3:
                            player_lives=2;
                            player_attack=1;
                            enemy_count=10;
                            enemy_velocity=.7;
                            enemy_shot_vel=.7;
                            enemy_attack=3;
                            ball_count=3;
                            ball_velocity=.8;
                            ball_time=500;
                            break;
                    }    
                    break;
                case 165:
                    game_mode++;
                    if(game_mode>1)game_mode=0;
                    if(game_mode)map_number=1;
                    else map_number=21;
                    break;    
           }
        }        
        else {
            if(!key[KEY_RIGHT]){
                keyhold[KEY_RIGHT]=0;
            }
        }        
        if(count1>0&&count1<2&&end_option==0)spr=OPTION0;
        if(count1>2&&count1<4&&end_option==0)spr=OPTION1;
        if(count1>4&&count1<6&&end_option==0)spr=OPTION2;
        if(count1>6&&count1<8&&end_option==0)spr=OPTION3;
        if(count1>8&&count1<10&&end_option==0)spr=OPTION4;
        if(count1>=10&&end_option==0){spr=OPTION5;end_option=1;}
        if(count1<10&&end_option==0)count1++;
        if(count2>0&&count2<2&&end_option==2)spr=OPTION5;
        if(count2>2&&count2<4&&end_option==2)spr=OPTION4;
        if(count2>4&&count2<6&&end_option==2)spr=OPTION3;
        if(count2>6&&count2<8&&end_option==2)spr=OPTION2;
        if(count2>8&&count2<10&&end_option==2)spr=OPTION1;
        if(count2>=10&&end_option==2){spr=OPTION0;end_option=3;}
        if(count2<10&&end_option==2)count2++; 
        scroll_x1-=6;
        scroll_x2+=6;
        if(scroll_x1<=0)scroll_x1=640;
        if(scroll_x2>=640)scroll_x2=0;
        game_timer--;
      }    
        blit(title_images[TITLE2].dat,buff,scroll_x1,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        blit(title_images[TITLE2].dat,buff,0,0,scroll_x2,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
        draw_sprite(buff,title_images[TITLE3].dat,bass_x,bass_y);
        draw_sprite(buff,title_images[TITLE4].dat,menu_x,menu_y);
        draw_sprite(buff,title_images[TITLE5].dat,select_x,select_y);
        draw_sprite(buff,option_menu[spr].dat,150,70);
        if(count1>=10&&end_option==1){
            draw_sprite(buff,option_menu[OPTION_SELECT].dat,160,selector);
            textprintf_centre_ex(buff,font,324,90,makecol(rand()%256,rand()%256,rand()%256),-1,"OPTIONS");
            textprintf_ex(buff,font,180,110,makecol(255,255,255),-1,"MUSIC   (%d):",music_vol);
            textprintf_ex(buff,font,180,130,makecol(255,255,255),-1,"EFFECTS (%d):",sound_vol);
            textprintf_ex(buff,font,180,150,makecol(255,255,255),-1,"DIFFICULTY:");
            switch(game_difficulty){
                case 0:
                  textprintf_ex(buff,font,270,150,makecol(255,255,0),-1,"EASY");
                  textprintf_ex(buff,font,312,150,makecol(255,255,255),-1,"NORMAL");
                  textprintf_ex(buff,font,372,150,makecol(255,255,255),-1,"HARD");
                  textprintf_ex(buff,font,415,150,makecol(255,255,255),-1,"VERY HARD");
                  break;
                case 1:
                  textprintf_ex(buff,font,270,150,makecol(255,255,255),-1,"EASY");
                  textprintf_ex(buff,font,312,150,makecol(255,255,0),-1,"NORMAL");
                  textprintf_ex(buff,font,372,150,makecol(255,255,255),-1,"HARD");
                  textprintf_ex(buff,font,415,150,makecol(255,255,255),-1,"VERY HARD");
                  break;
                case 2:
                  textprintf_ex(buff,font,270,150,makecol(255,255,255),-1,"EASY");
                  textprintf_ex(buff,font,312,150,makecol(255,255,255),-1,"NORMAL");
                  textprintf_ex(buff,font,372,150,makecol(255,255,0),-1,"HARD");
                  textprintf_ex(buff,font,415,150,makecol(255,255,255),-1,"VERY HARD");
                  break;
                case 3:
                  textprintf_ex(buff,font,270,150,makecol(255,255,255),-1,"EASY");
                  textprintf_ex(buff,font,312,150,makecol(255,255,255),-1,"NORMAL");
                  textprintf_ex(buff,font,372,150,makecol(255,255,255),-1,"HARD");
                  textprintf_ex(buff,font,415,150,makecol(255,255,0),-1,"VERY HARD");
                  break;
            }    
            textprintf_ex(buff,font,180,170,makecol(255,255,255),-1,"GAME TYPE :");
            if(game_mode){
                textprintf_ex(buff,font,280,170,makecol(255,255,255),-1,"CLASSIC");
                textprintf_ex(buff,font,360,170,makecol(255,255,0),-1,"CHANGED UP");  
            }
            else {
                textprintf_ex(buff,font,280,170,makecol(255,255,0),-1,"CLASSIC");
                textprintf_ex(buff,font,360,170,makecol(255,255,255),-1,"CHANGED UP");  
            } 
            textprintf_ex(buff,font,180,190,makecol(255,255,255),-1,"TEST MAP  (must be in 'maps/' dir)");
            textprintf_ex(buff,font,180,210,makecol(255,255,255),-1,"RESET DEFAULTS");
            textprintf_centre_ex(buff,font,324,250,makecol(255,0,0),-1,"ABOUT");
            textprintf_centre_ex(buff,font,324,270,makecol(255,255,0),-1,"BASSERK VERSION 1.2");
            textprintf_ex(buff,font,180,290,makecol(255,255,255),-1,"Berzerk clone/remake");
            textprintf_ex(buff,font,180,310,makecol(255,255,255),-1,"PROGRAMMING : Miguel");
            textprintf_ex(buff,font,180,330,makecol(255,255,255),-1,"MUSIC       : Rey");
            textprintf_ex(buff,font,180,350,makecol(255,255,255),-1,"ART & SOUNDS: COPYRIGHT CAPCOM LTD");
            textprintf_ex(buff,font,180,370,makecol(255,255,255),-1,"Done in C with the Allegro Library.");
            textprintf_ex(buff,font,180,390,makecol(255,255,255),-1,"Source available with archive.");
            rect(buff,294,109,295+MAX_VOLUME/3+1,120,makecol(255,255,255));
            rect(buff,294,129,295+MAX_VOLUME/3+1,140,makecol(255,255,255));
            rectfill(buff,bar1_x1,bar1_y1,bar1_x2,bar1_y2,makecol(255,255,0));
            rectfill(buff,bar2_x1,bar2_y1,bar2_x2,bar2_y2,makecol(255,255,0));
            rect(buff,165,246,480,400,makecol(0,0,255));
            
        } 
        blit(buff,screen,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);   
        //sleep(1);
    }   
    process=0; 
    destroy_bitmap(buff);
    unload_datafile(option_menu);
    unload_datafile(title_images);
    return tester;
}

void game_intro(int bass_num){
    int bass_x=bass_num,bass_y=120,
        menu_x=220,menu_y=-250,done=0,up=0,wait=0;
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    BITMAP *title;
    BITMAP *sec_buff;
    DATAFILE *title_images;
    title=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    sec_buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    title_images=load_datafile("data/title.dat");
    blit(title_images[TITLE0].dat,title,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
    screen_fade_in(title,10);
    rest(2000);
    screen_fade_out(10);
    blit(title_images[TITLE1].dat,title,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
    play_sample(game_sound[CAPCOM].dat, sound_vol, 128, 1000, 0);
    screen_fade_in(title,10);
    rest(2000);
    screen_fade_out(10);
    stop_sample(game_sound[CAPCOM].dat);
    process=1;
    while(!done){
       while(game_timer){
        if(key[KEY_ENTER]&&!keyhold[KEY_ENTER]){
            keyhold[KEY_ENTER]=1;
            done=1;
        }
        else {
            if(!key[KEY_ENTER]){
                keyhold[KEY_ENTER]=0;
            }
        }        
        if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
            keyhold[KEY_ESC]=1;
            done=1;
        }
        else {
            if(!key[KEY_ESC]){
                keyhold[KEY_ESC]=0;
            }
        }            
        if(bass_x>5)bass_x-=7;
        if(bass_x<5)bass_x=5;
        if(bass_x<=5&&!up)menu_y+=7;
        if(menu_y>320)up=1;
        if(up&&menu_y>140)menu_y-=6;
        if(up&&menu_y<140){menu_y=140;done=1;}
        if(key[KEY_ENTER]||key[KEY_ESC])done=1;
        scroll_x1-=6;
        scroll_x2+=6;
        if(scroll_x1<=0)scroll_x1=640;
        if(scroll_x2>=640)scroll_x2=0;
        game_timer--;
       }    
       if(wait<256)wait+=10;  
       blit(title_images[TITLE2].dat,title,scroll_x1,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
       blit(title_images[TITLE2].dat,title,0,0,scroll_x2,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
       draw_sprite(title,title_images[TITLE3].dat,bass_x,bass_y);
       draw_sprite(title,title_images[TITLE4].dat,menu_x,menu_y);
       if(wait<=256){
           clear(sec_buff);
           set_trans_blender(0,0,0,wait);
           draw_trans_sprite(sec_buff, title, 0, 0);
           blit(sec_buff,title,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
       }    
       blit(title,screen,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
       //sleep(1);
    }
    process=0; 
    destroy_bitmap(title);
    destroy_bitmap(sec_buff);
    unload_datafile(title_images);   
}

int game_menu(void){
    int bass_x=5,bass_y=120,
        menu_x=220,menu_y=140,done=0,
        select=0,select_count=0,select_spr=TITLE5,
        select_x=180,select_y=225,menu_wait=1000,
        exit=0;
    char *q_game="Quit Game?";
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
    keyhold[KEY_UP]=0;
    keyhold[KEY_DOWN]=0;
    keyhold[KEY_ESC]=0;
    BITMAP *title;
    DATAFILE *title_images;
    title=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    title_images=load_datafile("data/title.dat");
    process=1;
    while(!done){
     while(game_timer){
       if(key[KEY_ENTER]&&!keyhold[KEY_ENTER]){
           menu_wait=1000;
           keyhold[KEY_ENTER]=1;
           if(!select){
               done=1;exit=START_GAME;
           }    
           if(select){
               done=1;exit=OPTION_MENU;
           }    
       }    
       else {
           if(!key[KEY_ENTER]){
               keyhold[KEY_ENTER]=0;
           }
       }        
       if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
           keyhold[KEY_ESC]=1;
           process=0;
           if(yesno_win(screen,q_game,259,174)){
              done=1;exit=EXIT_GAME;
           }    
           process=1;
       }    
       else {
           if(!key[KEY_ESC]){
               keyhold[KEY_ESC]=0;
           }    
       }    
       if(key[KEY_UP]&&!keyhold[KEY_UP]){
           keyhold[KEY_UP]=1;
           if(select)select=0;
           else select=1;
           menu_wait=1000;
           play_sample(game_sound[SELECT_SOUND].dat,sound_vol,128,1000,0);
       }
       else {
            if(!key[KEY_UP]){
             keyhold[KEY_UP]=0;
            }    
       }    
       if(key[KEY_DOWN]&&!keyhold[KEY_DOWN]){
           keyhold[KEY_DOWN]=1;
           if(select)select=0;
           else select=1;
           menu_wait=1000;
           play_sample(game_sound[SELECT_SOUND].dat,sound_vol,128,1000,0);
       }
       else {
            if(!key[KEY_DOWN]){
             keyhold[KEY_DOWN]=0;
            }    
       }    
       select_count++;
       if(select)select_y=285;
       else select_y=225;
       if(select_count>0&&select_count<20)select_spr=TITLE5;
       if(select_count>20&&select_count<40)select_spr=TITLE6;
       if(select_count>40&&select_count<60)select_spr=TITLE7;
       if(select_count>60&&select_count<80)select_spr=TITLE6;
       if(select_count>80)select_count=0;
       menu_wait--;
       if(menu_wait<0){
           done=1;exit=RESTART_INTRO;
       }   
       scroll_x1-=6;
       scroll_x2+=6;
       if(scroll_x1<=0)scroll_x1=640;
       if(scroll_x2>=640)scroll_x2=0; 
       game_timer--;
     } 
     blit(title_images[TITLE2].dat,title,scroll_x1,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
     blit(title_images[TITLE2].dat,title,0,0,scroll_x2,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
     draw_sprite(title,title_images[TITLE3].dat,bass_x,bass_y);
     draw_sprite(title,title_images[TITLE4].dat,menu_x,menu_y);
     draw_sprite(title,title_images[select_spr].dat,select_x,select_y);
     blit(title,screen,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
     //sleep(1);
    }  
    process=0;
    if(exit!=OPTION_MENU){
       screen_fade_out(10);
       if(exit==RESTART_INTRO)draw_highscore();
       stop_sample(game_sound[TITLE_MUSIC].dat); 
    }    
    destroy_bitmap(title);
    unload_datafile(title_images); 
    return exit;
}   


    
void do_begin(int skip_intro){
    int stat=RESTART_INTRO,music_started=0,bass_starter=5000;
    while(stat==RESTART_INTRO){
     if(!skip_intro){
         if(music_started){stop_sample(game_sound[TITLE_MUSIC].dat);music_started=0;}
         game_intro(bass_starter);
         bass_starter=1000;
     }    
     skip_intro=0;
     if(!music_started){
         music_started=1;
         play_sample(game_sound[TITLE_MUSIC].dat,music_vol,128,1000,1);
     }
     stat = game_menu();
     if(stat==OPTION_MENU){
         while(main_option_win()){
             stop_sample(game_sound[TITLE_MUSIC].dat);
             test_map();
             play_sample(game_sound[TITLE_MUSIC].dat,music_vol,128,1000,1);
         }    
         skip_intro=1;
         stat=RESTART_INTRO;
     }
     if(stat==EXIT_GAME)game_quit=1;
    }   
}    

int do_game(void){
    int i,count=0,end_game=0,cur_level_done=0,win_level=0,last_energy,
        death_count=0,cur_enemy_count,increase_enemy_count=0,wait_enemy_shoot=0,
        music_started=0,exit_return=0;
    char *q_main="Quit to Main?";
    LOCK_VARIABLE(game_timer);
    LOCK_FUNCTION(timer_handle());
    install_int_ex(timer_handle,BPS_TO_TIMER(80));
        count_levels=1;
        save_temp_data();
        local[0].lives=player_lives;
        if(game_mode)local[0].energy=player_energy;
        else local[0].energy=1;
        last_energy=local[0].energy;
    while(!end_game){
        count=0;
        cur_enemy_count=0;
        cur_level_done=0;
        wait_enemy_shoot=0;
        if(!music_started){
            music_started=1;
            play_sample(game_sound[GAME_MUSIC].dat,music_vol,128,1000,1); 
        }    
        if(game_mode){
            char *res=get_map_number((rand()%20)+1);
            load_map(res);
        }    
        else {
            char *res=get_map_number((rand()%10)+21);
            load_map(res);
        }    
        init_enemies(enemy_count);
        for(i=0;i<MAX_ENEMY;i++){
           if(enemy[i].alive)cur_enemy_count++;
        }
        init_player(1);
        local[0].energy=last_energy;
        init_ball(ball_count,ball_time); 
        process=1;
        while(!cur_level_done){
          while(game_timer){
              if(key[KEY_ESC]&&!keyhold[KEY_ESC]){
                keyhold[KEY_ESC]=1;
                process=0;
                if(!help&&yesno_win(screen,q_main,259,174)){
                    cur_level_done=1;end_game=1;exit_return=1;
                    stop_sample(game_sound[GAME_MUSIC].dat);
                }
                if(help)help=0;
                process=1;            
              }  
              else {
                if(!key[KEY_ESC]){
                  keyhold[KEY_ESC]=0;
                }
              }
              if(key[KEY_F1]&&!keyhold[KEY_F1]){
                keyhold[KEY_F1]=1;
                if(help)help=0;
                else help=1;
              }
              else {
                if(!key[KEY_F1]){
                  keyhold[KEY_F1]=0;
                }
              } 
              if(!local[0].dead&&!help){
                player_control(1);
                update_player_shot(1,enemy_count);
                move_enemies(enemy_count,enemy_velocity);
                if(wait_enemy_shoot==300)enemy_shoot(enemy_count,local[0].x1,local[0].y1);
                update_enemy_shot(enemy_count,enemy_shot_vel);
                move_ball(ball_count,ball_velocity,local[0].cur_x,local[0].cur_y);
                update_enemy_explosion(enemy_count);
                update_ball_explosion(ball_count);
                if(game_mode){  
                  for(i=0;i<MAX_ENEMY;i++){
                     if(!enemy[i].alive)count++;
                     else count=0;
                  }
                  if(count>=MAX_ENEMY){
                     if(open_doors()>0)play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0); 
                     win_level++; 
                  }    
                  else {
                     if(close_doors()>0)play_sample(game_sound[DOOR].dat,sound_vol,128,1000,0);
                     win_level=0;
                  }    
                  if(win_level==1){
                    stop_sample(game_sound[GAME_MUSIC].dat);
                    play_sample(game_sound[WIN_MUSIC].dat,music_vol,128,1000,0);
                    music_started=0;
                  }    
                }
                if(local[0].cur_x<-50||local[0].cur_x>640||local[0].cur_y<-60||local[0].cur_y>400)cur_level_done=1;
                if(local[0].points>=free_life){
                    local[0].lives++;
                    play_sample(game_sound[FREE_MAN].dat,sound_vol,128,1000,0);
                    free_life+=free_life+1000;
                }    
              }
              add_points();  
              if(local[0].dead){
                  death_count++;
                  player_death(local[0].id);      
              }     
              if(death_count>=150){
                  death_count=0;cur_level_done=1;local[0].lives--;
              }   
              if(local[0].lives<0){
                  cur_level_done=1;end_game=1;
              }    
              if(wait_enemy_shoot<300)wait_enemy_shoot++;
              game_timer--;
          }  
          draw_map();
          draw_player(1);
          draw_enemies(enemy_count);
          draw_enemy_shot(enemy_count);
          draw_player_shot(1);
          draw_ball(ball_count);
          draw_enemy_explosion(enemy_count);
          draw_ball_explosion(ball_count);  
          if(help)draw_pause();  
          draw_buffer(buffer,0,0,0,0,EDIT_MAP_SIZE_W,EDIT_MAP_SIZE_H);
          if(help)draw_help_game_menu(HELP_GAME);
          else draw_game_menu();
          draw_buffer(menu_buffer,0,0,0,EDIT_MAP_SIZE_H,EDIT_MENU_SIZE_W,EDIT_MENU_SIZE_H);  
          //sleep(1);
        }  
        process=0;   
        if(!local[0].dead){
          count=0;
          for(i=0;i<MAX_ENEMY;i++){
             if(enemy[i].alive)count++;
          }
          if(!count){
            for(i=0;i<cur_enemy_count;i++){
                give_points(10);
            }
          }
          last_energy=local[0].energy;
          count_levels++;
          increase_enemy_count++;
          switch(game_difficulty){
              case 0:
               enemy_velocity+=.1;
               enemy_shot_vel+=.1;
               if(increase_enemy_count==10){
                  enemy_count++;
                  enemy_velocity=.5;
                  increase_enemy_count=0;
               }
               break;
              case 1:
               enemy_velocity+=.2;
               enemy_shot_vel+=.2;
               if(increase_enemy_count==5)ball_velocity+=.1;
               if(increase_enemy_count==10){
                  enemy_count+=2;
                  enemy_velocity=1;
                  increase_enemy_count=0;
               }
               break;
              case 2:
               enemy_velocity+=.3;
               enemy_shot_vel+=.3;
               if(increase_enemy_count==5)ball_velocity+=.2;
               if(increase_enemy_count==10){
                  enemy_count+=4;
                  enemy_velocity=1.5;
                  increase_enemy_count=0;
               }
               break;  
              case 3:
               enemy_velocity+=.4;
               enemy_shot_vel+=.4;
               if(increase_enemy_count==4)ball_velocity+=.3;
               if(increase_enemy_count==8){
                  enemy_count+=6;
                  enemy_velocity=2;
                  increase_enemy_count=0;
               }
               break; 
          }    
        }   
        else {
          if(game_mode)local[0].energy=player_energy;
          else local[0].energy=1;
          last_energy=local[0].energy;
        }    
        for(i=0;i<MAX_ENEMY;i++){
            wipe_enemy(enemy[i].id);
        }    
        
        for(i=0;i<1000;i++){           //Clear out any extra stuff so it doesn't carry over to the next screen
                update_player_shot(1,enemy_count);
                update_enemy_shot(enemy_count,enemy_shot_vel);
                update_enemy_explosion(enemy_count);
                update_ball_explosion(ball_count);
        }   
        if(local[0].lives<0){
            draw_game_over(buffer);
        }    
        else {
            if(!end_game)screen_close(buffer);
        }      
    }   
    for(i=0;i<14;i++){
          if(i==12)i++;
          stop_sample(game_sound[i].dat);
    }   
    load_temp_data();
    local[0].points=0;
    process=1;
    screen_fade_out(16);
    return exit_return; 
} 

void timer_handle(void){
    if(process)game_timer++;
}END_OF_FUNCTION(timer_handle);     

void reset_default(void){
    show_rects=0;
    help=0;
    map_number=21;
    enemy_velocity=.5;
    enemy_shot_vel=.5;
    ball_velocity=.5;
    music_vol=200;
    sound_vol=255;
    game_mode=0;
    game_difficulty=1;
    player_lives=3;
    player_energy=55;
    player_attack=6;
    enemy_energy=55;
    enemy_attack=1;
    enemy_count=5;
    ball_count=1;
    ball_time=1000;
    game_start=0;
    free_life=2000,
    point_adder=0;
}

extern void give_points(int points){
    point_adder+=points;    
}

int save_temp_data(void){
    temp_show_rects=show_rects;
    temp_help=help;
    temp_map_number=map_number;
    temp_enemy_velocity=enemy_velocity;
    temp_enemy_shot_vel=enemy_shot_vel;
    temp_ball_velocity=ball_velocity;
    temp_music_vol=music_vol;
    temp_sound_vol=sound_vol;
    temp_game_mode=game_mode;
    temp_game_difficulty=game_difficulty;
    temp_player_lives=player_lives;
    temp_player_energy=player_energy;
    temp_player_attack=player_attack;
    temp_enemy_energy=enemy_energy;
    temp_enemy_attack=enemy_attack;
    temp_enemy_count=enemy_count;
    temp_ball_count=ball_count;
    temp_ball_time=ball_time;
    temp_game_start=game_start;
    temp_free_life=free_life;
    temp_point_adder=point_adder;
    return 0;
}

int load_temp_data(void){
    show_rects=temp_show_rects;
    help=temp_help;
    map_number=temp_map_number;
    enemy_velocity=temp_enemy_velocity;
    enemy_shot_vel=temp_enemy_shot_vel;
    ball_velocity=temp_ball_velocity;
    music_vol=temp_music_vol;
    sound_vol=temp_sound_vol;
    game_mode=temp_game_mode;
    game_difficulty=temp_game_difficulty;
    player_lives=temp_player_lives;
    player_energy=temp_player_energy;
    player_attack=temp_player_attack;
    enemy_energy=temp_enemy_energy;
    enemy_attack=temp_enemy_attack;
    enemy_count=temp_enemy_count;
    ball_count=temp_ball_count;
    ball_time=temp_ball_time;
    game_start=temp_game_start;
    free_life=temp_free_life;
    point_adder=temp_point_adder;
    return 0;
}  

#endif   
